//
//  DisherArray.m
//  Recipe
//
//  Created by bing han on 12-7-20.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "DisherArray.h"
#import "DBOptions.h"
#import "BaseUrl.h"

@implementation DisherArray
static DisherArray *instence = nil;
@synthesize disherArray = _disherArray;

+(DisherArray *) newInstence{
    @synchronized(self){
        if (instence == nil) {
            instence = [[DisherArray alloc] init];
            [instence initDisherArray];
        }
    }
    return instence;
}

+(id) allocWithZone:(NSZone *)zone{
    @synchronized(self){
        if (instence == nil) {
            instence = [super allocWithZone:zone];
            return instence;
        }
    }
    return nil;
}

-(id) copyWithZone:(NSZone *)zone{
    return self;
}

-(id) retain{
    return self;
}

-(unsigned) retainCount{
    return UINT_MAX;
}

-(id) autorelease{
    return self;
}

-(void) initDisherArray{
    DBOptions *dbOption = [DBOptions newInstence];
    FMDatabase *fmdb = dbOption.fmdatabase;
    _disherArray = [[NSMutableArray alloc] init];
    
    FMResultSet *rsdisher = [fmdb executeQuery:@"select * from dishes order by type"];
    while ([rsdisher next]) {
        NSMutableDictionary *result = [[NSMutableDictionary alloc] init];
        [result setValue:[rsdisher objectForColumnName:@"businesskey"] forKey:@"businesskey"];
        [result setValue:[rsdisher objectForColumnName:@"type"] forKey:@"type"];
        [result setValue:[rsdisher objectForColumnName:@"cnamecn"] forKey:@"cnamecn"];
        [result setValue:[rsdisher objectForColumnName:@"cnameen"] forKey:@"cnameen"];
        [result setValue:[rsdisher objectForColumnName:@"remarks"] forKey:@"remarks"];
        
        NSString *smallfile = [[rsdisher objectForColumnName:@"businesskey"] stringByAppendingString:@"small"];
        smallfile = [DISHERIMAGEPATH stringByAppendingString:smallfile];
        [result setValue:smallfile forKey:@"smallimage"];
        
        NSString *bigfile = [[rsdisher objectForColumnName:@"businesskey"] stringByAppendingString:@"big"];
        bigfile = [DISHERIMAGEPATH stringByAppendingString:bigfile];
        [result setValue:bigfile forKey:@"bigimage"];
        
//        NSString *smallfile = [[rsdisher objectForColumnName:@"businesskey"] stringByAppendingString:@"small"];
//        smallfile = [DISHERIMAGEPATH stringByAppendingString:smallfile];
//        UIImage *smallimage = [UIImage imageWithContentsOfFile:smallfile];
//        [result setValue:smallimage forKey:@"smallimage"];
//        
//        NSString *bigfile = [[rsdisher objectForColumnName:@"businesskey"] stringByAppendingString:@"big"];
//        bigfile = [DISHERIMAGEPATH stringByAppendingString:bigfile];
//        UIImage *bigimage = [UIImage imageWithContentsOfFile:bigfile];
//        [result setValue:bigimage forKey:@"bigimage"];
        
        [result setValue:[rsdisher objectForColumnName:@"cprice"] forKey:@"cprice"];
        [_disherArray addObject:result];
        [result release];
    }
    [dbOption closeDatabase];
}

@end
